﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TierService;
using TierApplication;
using System.Data;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxGridView.Rendering;
using System.Web.Services;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxClasses.Internal;

public partial class System_Product_ProductList : BasePage
{
    #region private
    private const string inputQueryStringPageModeKey = "pagemode";
    private const string currentPageModeKey = "CurrentPageMode";
    public enum PageMode
    {
        add,
        update,
        query,
        select
    }
    private Nullable<PageMode> pm;


    public enum enuDisplayMode
    {
        Add = 0,
        Edit = 1,
        Delete = 2,
        Init = 3
    }

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindBandwidth();
            BindLifeTime();
            BindSchool();
            BindCategoryId();

            string sModuleID = Page.Request.QueryString["MOUDLEID"];
            string pagemode = Page.Request.QueryString[inputQueryStringPageModeKey];
            if (pagemode == null) pagemode = "";
            switch (pagemode.ToLower().Trim())
            {
                case "add":
                    pm = PageMode.add;
                    break;
                case "update":
                    pm = PageMode.update;
                    break;
                case "query":
                    pm = PageMode.query;
                    break;
                case "select":
                    pm = PageMode.select;
                    break;
                default:
                    pm = PageMode.add;
                    break;
            }
            ViewState[currentPageModeKey] = pm;

            BindData();
        }
        else
        {
            pm = ViewState[currentPageModeKey] as Nullable<PageMode>;
            if (gridSearchResult.IsCallback)
            {
                System.Data.DataTable dt = (DataTable)ViewState["data"];
                gridSearchResult.DataSource = dt;
                gridSearchResult.DataBind();
            }
        }


    }

    #region BindData

    private void BindBandwidth()
    {
        DataSet ds = P_DDCODE.GetListByType(P_DDCODE.DDType.Bandwidth.ToString());
        this.ddlBANDWIDTH.DataSource = ds.Tables[0].DefaultView;
        this.ddlBANDWIDTH.TextField = "CodeName";
        this.ddlBANDWIDTH.ValueField = "CodeID";
        this.ddlBANDWIDTH.DataBind();
        this.ddlBANDWIDTH.Items.Insert(0, new ListEditItem("(全部)", ""));
    }

    private void BindLifeTime()
    {
        DataSet ds = P_DDCODE.GetListByType(P_DDCODE.DDType.LifeTime.ToString());
        this.ddlLIFETIME.DataSource = ds.Tables[0].DefaultView;
        this.ddlLIFETIME.TextField = "CodeName";
        this.ddlLIFETIME.ValueField = "CodeID";
        this.ddlLIFETIME.DataBind();
        this.ddlLIFETIME.Items.Insert(0, new ListEditItem("(全部)", ""));
    }

    private void BindCategoryId()
    {
        DataSet ds = P_DDCODE.GetListByType(P_DDCODE.DDType.CategoryId.ToString());
        this.ddlCATEGORYID.DataSource = ds.Tables[0].DefaultView;
        this.ddlCATEGORYID.TextField = "CodeName";
        this.ddlCATEGORYID.ValueField = "CodeID";
        this.ddlCATEGORYID.DataBind();
        this.ddlCATEGORYID.Items.Insert(0, new ListEditItem("(全部)", ""));
    }

    private void BindSchool()
    {
        T_SCHOOL mSchool = new T_SCHOOL();
        DataSet ds = mSchool.GetList("");
        this.ddlSCHOOLID.DataSource = ds;
        this.ddlSCHOOLID.ValueField = "SCHOOLID";
        this.ddlSCHOOLID.TextField = "SCHOOLNAME";
        this.ddlSCHOOLID.DataBind();
        this.ddlSCHOOLID.Items.Insert(0, new ListEditItem("(全部)", ""));
    }

    private void BindData()
    {
        #region 权限检查

        #endregion

        string ProductID = this.txtProductId.Text.Trim();
        string ProductName = this.txtPRODUCTNAME.Text.Trim();
        string SCHOOLID = (string)this.ddlSCHOOLID.SelectedItem.Value;
        string BANDWIDTH = (string)this.ddlBANDWIDTH.SelectedItem.Value;
        string LIFETIME = (string)this.ddlLIFETIME.SelectedItem.Value;
        string CATEGORYID = (string)this.ddlLIFETIME.SelectedItem.Value;
        string strWhere = "";

        if (ProductID != "")
        {
            strWhere += " and ProductID like'%" + ProductID + "%'";
        }
        if (ProductName != "")
        {
            strWhere += " and ProductName like'%" + ProductName + "%'";
        }
        if (SCHOOLID != "")
        {
            strWhere += " and SCHOOLID ='" + SCHOOLID + "'";
        }
        if (BANDWIDTH != "")
        {
            strWhere += " and BANDWIDTH ='" + BANDWIDTH + "'";
        }
        if (LIFETIME != "")
        {
            strWhere += " and LIFETIME ='" + LIFETIME + "'";
        }
        if (CATEGORYID != "")
        {
            strWhere += " and CATEGORYID ='" + CATEGORYID + "'";
        }


        T_PRODUCT mPRODUCT = new T_PRODUCT();
        DataSet ds = new DataSet();
        ds = mPRODUCT.GetProductList(strWhere);
        gridSearchResult.DataSource = ds.Tables[0];
        gridSearchResult.DataBind();
        ViewState["data"] = ds.Tables[0];
    }
    #endregion

    #region btn_Search

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void btnQuery_CallBack(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
    {
        BindData();
    }



    #endregion

    #region gridView事件
    protected void gridSearchResult_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {

        String theID = e.Keys[0].ToString();
        object theValues = this.gridSearchResult.GetRowValuesByKeyValue(theID, new String[] { "PRODUCTID" });
        String ProductID = theValues.ToString();
        if (!String.IsNullOrEmpty(ProductID))
        {
            T_PRODUCT mProduct = new T_PRODUCT();
            int iResult = 0;
            string sResult = "";
            mProduct.SP_T_PRODUCT_DELETE(ProductID,ref iResult,ref sResult);
            if (iResult == 0)
            {
                e.Cancel = true;
                this.gridSearchResult.CancelEdit();
                BindData();
            }
            else
            {                
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('数据删除成功');</script>");
            }
        }
        
    }


    protected void btnRowDeleteCallBack_CallBack(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
    {
        string ac_ProductID = e.Parameter;
        Int32 ai_result = 0;
        string ac_result = "";

        T_PRODUCT mProduct = new T_PRODUCT();
        mProduct.SP_T_PRODUCT_DELETE(ac_ProductID, ref ai_result, ref ac_result);

        if (ai_result == 0)
        {
            e.Result = "ok";
        }
        else
        {
            e.Result = ac_result;
        }
    }



    protected void gridSearchResult_HtmlRowCreated(object sender,
    ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == GridViewRowType.Data)
        {

            GridViewCommandColumn objgcc = getCommandColumnLoop();
            GridViewCommandColumnButtonControl thebtn;
            InternalHyperLink theIHL;
            Boolean checkAddPerm = false;
            Boolean checkDeletePerm = false;
            Boolean checkEditPerm = false;
            Boolean checkQueryPerm = false;
            Boolean checkSelectPerm = false;

           //pm = ViewState[currentPageModeKey] as Nullable<PageMode>;

            if (pm == PageMode.select)
            {
                checkAddPerm = false;
                checkDeletePerm = false;
                checkEditPerm = false;
                checkEditPerm = false;
                checkQueryPerm = true;
                checkSelectPerm = true;
            }
            if (pm != PageMode.select)
            {
                checkAddPerm = true;
                checkDeletePerm = true;
                checkEditPerm = true;
                checkEditPerm = true;
                checkQueryPerm = true;
                checkSelectPerm = false;
            }

            btnProductCreate.Visible = checkAddPerm;
            //delete
            thebtn = (GridViewCommandColumnButtonControl)e.Row.Cells[objgcc.VisibleIndex].Controls[0];
            thebtn.Enabled = checkDeletePerm;
            thebtn.Visible = checkDeletePerm;
            theIHL = (InternalHyperLink)thebtn.Controls[0];
            theIHL.Enabled = checkDeletePerm;
            theIHL.Visible = checkDeletePerm;

            //edit
            thebtn = (GridViewCommandColumnButtonControl)e.Row.Cells[objgcc.VisibleIndex].Controls[1];
            thebtn.Enabled = checkEditPerm;
            thebtn.Visible = checkEditPerm;
            theIHL = (InternalHyperLink)thebtn.Controls[0];
            theIHL.Enabled = checkEditPerm;
            theIHL.Visible = checkEditPerm;

            //query
            thebtn = (GridViewCommandColumnButtonControl)e.Row.Cells[objgcc.VisibleIndex].Controls[2];
            thebtn.Enabled = checkQueryPerm;
            thebtn.Visible = checkQueryPerm;
            theIHL = (InternalHyperLink)thebtn.Controls[0];
            theIHL.Enabled = checkQueryPerm;
            theIHL.Visible = checkQueryPerm;
            //select
            thebtn = (GridViewCommandColumnButtonControl)e.Row.Cells[objgcc.VisibleIndex].Controls[3];
            thebtn.Enabled = checkSelectPerm;
            thebtn.Visible = checkSelectPerm;
            theIHL = (InternalHyperLink)thebtn.Controls[0];
            theIHL.Enabled = checkSelectPerm;
            theIHL.Visible = checkSelectPerm;
        }


    }

    private GridViewCommandColumn getCommandColumnLoop()
    {
        GridViewCommandColumn theCommandColumn = null;
        foreach (GridViewColumn item in gridSearchResult.VisibleColumns)
        {
            if (item.GetType() == typeof(GridViewCommandColumn))
            {
                theCommandColumn = (GridViewCommandColumn)item;
                break;
            }
        }
        return theCommandColumn;
    }

    #endregion

}